package com.liming.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import com.liming.dao.GoodsInfodao;
import com.liming.dao.GoodsTypedao;
import com.liming.dao.Goodsdao;
import com.liming.model.Goods;
import com.liming.model.GoodsInfo;
import com.liming.serviceinterface.GoodsService;

/**
 * 
 * @author chengw
 * @version 20190628
 */
public class GoodsServiceImpl implements GoodsService {

	private Goodsdao goodsdao = new Goodsdao();
	private GoodsInfodao goodsDetaildao = new GoodsInfodao();
	private GoodsTypedao goodsTypeDao = new GoodsTypedao();
	
	@Override
	public List<Goods> getGoodsByName(String name) {
		// TODO Auto-generated method stub
		try {
			return goodsdao.getGoodsByName(name);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			return new ArrayList<Goods>();
		}
	}

	@Override
	public List<Goods> getHotGoods() {
		// TODO Auto-generated method stub
		try {
			return goodsdao.getHotGoods();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			return new ArrayList<Goods>();
		}
	}

	@Override
	public List<Goods> getGoodsByPrice(int min, int max) {
		// TODO Auto-generated method stub
		try {
			return goodsdao.getGoodsByPrice(min,max);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			return new ArrayList<Goods>();
		}
	}

	@Override
	public List<Goods> getGoodsByType(int typeid) {
		// TODO Auto-generated method stub	
		ArrayList<Goods> result = new ArrayList<Goods>();
		Queue<Integer> queue = new LinkedList<>();
		queue.add(typeid);
		while(!queue.isEmpty()) {
			int queueHead = queue.poll();
			try {
				result.addAll(goodsdao.getGoodsByType(queueHead));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return result;
			}
			queue.addAll(goodsTypeDao.getTypeidsByPid(queueHead));
		}
		return result;
	}
	

	@Override
	public GoodsInfo getGoodsInfo(int goodsid) {
		// TODO Auto-generated method stub
		try {
			return goodsDetaildao.getGoodsDetail(goodsid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			return GoodsInfo.NULLGOODS;
		}
	}

}
